<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="设备名称：">
          <el-input v-model="searchForm.videoName" placeholder="设备名称"></el-input>
        </el-form-item>
        <el-form-item label="设备编号：">
          <el-input v-model="searchForm.videoCode" placeholder="设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备状态：">
          <el-select v-model="searchForm.videoStatus" placeholder="请选择">
            <el-option v-for="item in doorStatus" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型：">
          <el-select v-model="searchForm.deviceType" placeholder="请选择">
            <el-option v-for="item in deviceTypes" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期选择：">
          <el-date-picker v-model="dateList" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button v-if="permissions.hik_equipment_add" icon="el-icon-plus" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="permissions.hik_equipment_export" icon="el-icon-download" type="primary" plain
            @click="exportExcel()">导出</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList()" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="clean()" icon="el-icon-search">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column prop="videoName" header-align="center" align="center" label="设备名称">
          </el-table-column>
          <el-table-column prop="videoCode" header-align="center" align="center" label="设备编码">
          </el-table-column>
          <el-table-column prop="ipAddress" header-align="center" align="center" label="IP地址">
          </el-table-column>
          <el-table-column prop="username" header-align="center" align="center" label="用户名">
          </el-table-column>
          <el-table-column prop="password" header-align="center" align="center" label="密码">
          </el-table-column>
          <el-table-column prop="portNumber" header-align="center" align="center" label="端口号">
          </el-table-column>
          <el-table-column prop="encodingMethod" header-align="center" align="center" label="编码方式">
          </el-table-column>
          <el-table-column prop="bitRate" header-align="center" align="center" label="码率">
          </el-table-column>
          <el-table-column prop="deviceType" header-align="center" align="center" :formatter="doorTypes" label="设备类型">
          </el-table-column>
          <el-table-column prop="deviceStatus" header-align="center" align="center" :formatter="doorStatuses"
            label="设备状态">
          </el-table-column>
          <el-table-column prop="deviceLocation" header-align="center" align="center" label="设备位置">
          </el-table-column>
          <el-table-column prop="realTimeVideo" header-align="center" align="center" label="实时视频">
          </el-table-column>
          <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
          </el-table-column>
          <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
          </el-table-column>
          <el-table-column prop="updateBy" header-align="center" align="center" label="操作人">
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button v-if="permissions.hik_equipment_edit" type="text" size="small" icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.videoId)">修改</el-button>
              <el-button v-if="permissions.hik_equipment_del" type="text" size="small" icon="el-icon-delete"
                @click="deleteHandle(scope.row.videoId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj } from '@/api/hik/equipment'
import TableForm from './equipment-form'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      doorStatus: [{
        value: '0',
        label: '在线'
      }, {
        value: '1',
        label: '离线'
      }],
      deviceTypes: [{
        value: '1',
        label: '门禁'
      }, {
        value: '2',
        label: '闸机'
      }, {
        value: '3',
        label: '摄像头'
      }, {
        value: '4',
        label: '人脸设备'
      }, {
        value: '5',
        label: '道闸'
      }, {
        value: '6',
        label: '车牌识别'
      }],
      dateList: [],
      searchForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    TableForm
  },
  created() {
    this.getDataList()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    doorStatuses(row, column) {
      if (row.videoStatus === '0') {
        return '在线'
      } else {
        return '离线'
      }
    },
    doorTypes(row, column) {
      if (row.deviceType === '1') {
        return '门禁'
      } else if (row.deviceType === '2') {
        return '闸机'
      } else if (row.deviceType === '3') {
        return '摄像头'
      } else if (row.deviceType === '4') {
        return '人脸设备'
      } else if (row.deviceType === '5') {
        return '道闸'
      } else if (row.deviceType === '6') {
        return '车牌识别'
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      if (this.dateList != null && this.dateList.length > 1) {
        this.searchForm.startTime = this.dateList[0];
        this.searchForm.endTime = this.dateList[1];
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }
      fetchList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      }, this.searchForm)).then(response => {
        this.dataList = response.data.data.records
        this.totalPage = response.data.data.total
      })
      this.dataListLoading = false
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/hik/equipment/export', this.searchForm, '设备管理.xlsx')
    },
    clean() {
      this.searchForm = {}
      this.dateList = []
    }
  }
}
</script>
